<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:notice:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAdd">发布通知公告
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" border>
      <el-table-column align="center" label="标题" prop="noticeTitle" width="250px;"/>
      <el-table-column align="center" label="类型" prop="noticeType" width="100px;">
        <template slot-scope="scope">
          <dict-tag :options="noticeTypeOptions" :value="parseInt(scope.row.noticeType)"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="内容" prop="noticeContent"/>
      <el-table-column align="center" label="发布人" prop="createName" width="100px;"/>
      <el-table-column align="center" label="发布时间" prop="createTime" width="160px;"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="160px;">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:notice:query']" size="mini" type="text"
                     @click="handleViewConfirm(scope.row)">查看确认
          </el-button>
          <el-button v-hasPermi="['cld:notice:query']" size="mini" type="text"
                     @click="handleView(scope.row)">详情
          </el-button>
          <el-button v-hasPermi="['cld:notice:remove']" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改通知公告对话框 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="open" append-to-body title="通知公告" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-form-item label="选择项目" prop="projectIdArr">
          <el-select v-model="form.projectIdArr" clearable multiple placeholder="请选择">
            <el-option
              v-for="item in projectOptions"
              :key="item.projectId"
              :label="item.projectName"
              :value="item.projectId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择职务" prop="duitArr">
          <el-select v-model="form.duitArr" clearable multiple placeholder="请选择">
            <el-option v-for="dict in dict.type.cld_project_duits" :key="dict.value" :label="dict.label"
                       :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="noticeTitle">
          <el-input v-model="form.noticeTitle" :disabled="form.noticeId" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="类型" prop="noticeType">
          <el-select v-model="form.noticeType" :disabled="form.noticeId" placeholder="请选择">
            <el-option v-for="item in noticeTypeOptions" :key="item.value" :label="item.label"
                       :value="parseInt(item.value)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="noticeContent">
          <el-input v-model="form.noticeContent" :disabled="form.noticeId" :rows="8" placeholder="请输入"
                    type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!form.noticeId" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看确认明细 -->
    <el-dialog :title="title" :visible.sync="openConfirmOpen" width="80%" append-to-body>
      <confirm-list v-if="openConfirmOpen" :form="currRow"></confirm-list>
    </el-dialog>
  </div>
</template>

<script>
import {addNotice, delNotice, getNotice, listNotice, updateNotice,getNoticeConfirmList} from "@/api/cld/notice";
import {noticeTypeOptions} from '@/assets/staticDict'
import {getList} from '@/api/cld/project'
import confirmList from "@/views/cld/notice/confirmList.vue";

export default {
  name: "Notice",
  dicts: ['cld_project_duits'],
  components: {confirmList},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 通知公告表格数据
      noticeList: [],
      // 通知公告确认明细表格数据
      currRow: {},
      noticeConfirmList: [],
      // 是否显示弹出层
      open: false,
      openConfirmOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticeTitle: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        noticeType: [
          {required: true, message: "类型不能为空", trigger: "blur"}
        ],
        noticeContent: [
          {required: true, message: "内容不能为空", trigger: "blur"}
        ],
      },
      // 通知公告类型
      noticeTypeOptions: noticeTypeOptions,
      projectOptions: []
    };
  },
  created() {
    this.getList();
    this.handleGetProjectList();
  },
  methods: {
    /** 查询通知公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 加载项目
    handleGetProjectList(e) {
      getList({
        projectStatus: 2
      }).then(res => {
        this.projectOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: null,
        noticeTitle: null,
        noticeContent: null,
        noticeType: 1,
        projectIdArr: [],
        duitArr: [],
        projectIds: null,
        duits: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 查看 */
    handleView(row) {
      this.reset();
      getNotice(row.noticeId).then(response => {
        this.form = response.data;
        this.form.projectIdArr = this.form.projectIds?.split(',').map(Number);
        this.form.duitArr = this.form.duits?.split(',').map(Number);
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.projectIds = this.form.projectIdArr.join(',');
          this.form.duits = this.form.duitArr.join(',');
          if (this.form.noticeId != null) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids;
      this.$modal.confirm('是否确认删除通知公告编号为"' + noticeIds + '"的数据项？').then(function () {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 查看确认明细
    handleViewConfirm(row){
      this.title = row.noticeTitle + '确认列表';
      this.currRow = row;
      this.openConfirmOpen = true;
    },
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}
</style>
